package com.itheima.reggie.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.utils.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    
    
    //简单的处理办法就是在这里注入进来HTTPSession
    @Autowired
    private HttpSession session;
    
    public void insertFill(MetaObject metaObject ) {
        metaObject.setValue("createTime" , LocalDateTime.now());
        metaObject.setValue("updateTime" , LocalDateTime.now());
        
        //可以从session里面取出来员工对象，然后得到id值
        Employee employee  = (Employee) session.getAttribute("employee");
        
        //换成ThreadLocal的写法！
        metaObject.setValue("createUser" , BaseContext.get());
        metaObject.setValue("updateUser" ,  BaseContext.get());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime" , LocalDateTime.now());
    
        Employee employee  = (Employee) session.getAttribute("employee");
    
        //换成ThreadLocal的写法！
        metaObject.setValue("updateUser" , BaseContext.get());
    }
}
